package com.xxxx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.common.result.RespBean;
import com.xxxx.common.utils.JsonUtil;
import com.xxxx.manager.mapper.GoodsMapper;
import com.xxxx.manager.pojo.Goods;
import com.xxxx.manager.pojo.GoodsExample;
import com.xxxx.manager.pojo.query.GoodsQuery;
import com.xxxx.manager.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 保存商品通用信息
     */
    @Override
    public void saveGoods(Goods goods) {

        goodsMapper.insertSelective(goods);

    }

    /**
     * 商品列表分页查询
     */
    @Override
    public Map<String, Object> queryGoodsList(GoodsQuery goodsQuery, Integer page, Integer limit) {
        Map<String,Object> map = new HashMap<>();

        // 无条件查询：
        // goods:page_:limit_:catId_:brandId_:goodsName_:

        // 条件查询:
        // goods:page_:limit_:catId_123:brandId_:goodsName_:
        // goods:page_:limit_:catId_:brandId_123:goodsName_:
        // goods:page_:limit_:catId_:brandId_:goodsName_华为:
        // goods:page_:limit_:catId_123:brandId_245:goodsName_:
        // goods:page_:limit_:catId_123:brandId_:goodsName_华为:
        // goods:page_:limit_:catId_:brandId_245:goodsName_华为:
        // goods:page_:limit_:catId_123:brandId_245:goodsName_华为:

        // 把key放到数组中
        String[] goodsKeys = {
                "goods:page_"+page+":limit_"+limit+":",
                "catId_:",
                "brandId_:",
                "goodsName_:"
        };

        if(goodsQuery.getCatId() != null) {
            goodsKeys[1] = "catId_" + goodsQuery.getCatId()+":";
        }
        if(goodsQuery.getBrandId() != null) {
            goodsKeys[2] = "brandId_" + goodsQuery.getBrandId()+":";
        }
        if(goodsQuery.getGoodsName() != null) {
            goodsKeys[3] = "goodsName_" + goodsQuery.getGoodsName()+":";
        }

        // 拼接goodsKeys
        String goodsKey = Arrays.stream(goodsKeys).collect(Collectors.joining());
        // System.out.println(goodsKeysList);  // goods:page_1:limit_10:catId_:brandId_:goodsName_:

        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        String s = valueOperations.get(goodsKey);
        // 如果不为空
        if(!StringUtils.isEmpty(s)) {
            PageInfo pageInfo = JsonUtil.jsonStr2Object(s, PageInfo.class);
            map.put("code",200);
            map.put("list",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            map.put("goodsPageInfo",pageInfo);
            return map;
        }

        // 开启分页
        PageHelper.startPage(page, limit);

        // 得到对应的商品列表
        List<Goods> goodsPage = goodsMapper.queryGoodsList(goodsQuery);

        // 得到分页对象
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsPage);
        valueOperations.set(goodsKey,JsonUtil.object2JsonStr(goodsPageInfo));
        map.put("code",200);
        map.put("msg", "");
        map.put("list",goodsPageInfo.getTotal());
        map.put("data",goodsPageInfo.getList());
        map.put("goodsPageInfo",goodsPageInfo);

        return map;

    }

}
